package com.wn.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wn.bean.Student;
import com.wn.service.StudentService;
import com.wn.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -06-30
 * @desc
 */
@WebServlet("/queryStus")
public class QueryStusServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        // 01 拿到数据

        // 02 调用业务层
        StudentService studentService = new StudentServiceImpl();
        List<Student> students = studentService.queryStudents();
        // 03 回写数据
        // 01 把java对象转成 json 字符串
        // 02 注意乱码操作
        ObjectMapper objectMapper = new ObjectMapper();

        String s = objectMapper.writeValueAsString(students);
        response.getWriter().write(s);

    }
}
